package com.qianxun.module.datasource1.admin.rest;

import com.qianxun.module.base.bo.ValidGroup;
import com.qianxun.module.datasource1.admin.entity.SysUser;
import com.qianxun.module.datasource1.admin.service.SysMenuService;
import com.qianxun.module.datasource1.admin.entity.SysMenu;
import com.qianxun.module.datasource1.admin.dto.SysMenuDTO;
import com.qianxun.module.base.vo.MenuTreeVo;
import com.qianxun.module.base.constant.Result;
import com.qianxun.framework.common.util.ObjectUtil;
import com.qianxun.module.base.bo.ZTreeNode;
import com.qianxun.plugins.shiro.ShiroUtil;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
* com.zch.admin.base.entity.SysMenu模型http接口处理
*
* @author: admin
* @date: 2019-11-14
* @version: 1.0.0
*/
@RestController
@RequestMapping("/admin/menu")
public class SysMenuRestController {

    @Resource
    private SysMenuService baseService;

    @PostMapping(value = {"/add"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Result add(@Validated({ValidGroup.C1.class}) SysMenuDTO query) {
        baseService.add(query);
        return Result.success();
    }

    @PostMapping(value = {"/delete/id/{id}"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Result deleteById(@PathVariable(value = "id") Long id) {
        baseService.deleteSelfAndChildrenById(id);
        return Result.success();
    }

    @PostMapping(value = {"/update/id/{id}"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Result updateById(@PathVariable(value = "id") Long id, SysMenuDTO query) {
        SysMenu SysMenu = new SysMenu();
        ObjectUtil.switchAllFieldValue(SysMenu, query);
        baseService.updateById(SysMenu);
        return Result.success();
    }

    @RequestMapping(value = {"/search/all"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Result searchAll() {
        List<SysMenu> list = baseService.getAllAndOrder();
        return Result.success(list);
    }

    @RequestMapping(value = {"/search/id/{id}"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Result searchOneById(@PathVariable(value = "id") Long id) {
        SysMenu SysMenu = baseService.getById(id);
        return Result.success(SysMenu);
    }

    /**
     * 后台首页菜单导航树
     * @return
     */
    @RequestMapping(value = {"/search/vertical/tree"} ,produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Result searchVerticalTree(){
        SysUser sysUser = ShiroUtil.getUser(SysUser.class);;
        List<MenuTreeVo> list = baseService.searchVerticalMenuTree(sysUser.getId());
        return Result.success(list);
    }

    @RequestMapping(value = {"/search/level/tree/{id}"})
    public Result searchLevelTree(@PathVariable("id") long id){
        SysUser sysUser = ShiroUtil.getUser(SysUser.class);;
        List<MenuTreeVo> list = baseService.searchLevelMenuTree(sysUser.getId() ,id);
        return Result.success(list);
    }

    /**
     * 用户角色分配树
     * @param roleId
     * @return
     */
    @RequestMapping(value = {"/search/tree/{roleId}"} ,produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public String searchMenuTreeByRoleId(@PathVariable(value = "roleId") Long roleId){
        List<ZTreeNode> list = baseService.searchMenuTreeByRoleId(roleId);
        return ObjectUtil.toJson(list);
    }
}
